//cookie操作
var cookie = {
    set: function (key, value, minutes) {
        //document.cookie=key+"="+encodeURI(value,"utf-8");
        value = encodeURI(value, "utf-8");
        var d = new Date();
        d.setTime(d.getTime() + (minutes * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = key + "=" + value + "; " + expires;
    },
    get: function (key) {
        var cookies = document.cookie;
        var cookeArray = cookies.split(";");
        for (var i = 0; i < cookeArray.length; i++) {
            var cooke = cookeArray[i].trim();
            var index = cooke.indexOf("=");
            var keyre = cooke.substring(0, index);
            if (keyre === key) {
                var value = cooke.substring(index + 1);
                return decodeURI(value);
            }
        }
    },
    del: function (name) {
        cookie.set(name, "", -1);
    }
};

//头部导航条点击之后,保留颜色
function clickTitle(ob) {
    var $ = layui.jquery;
    var ind = $(ob).parent().index();
    cookie.set("titleIndex", ind, 0.5);
}

/**获取表单数据**/
function getFormData(formId) {
    var $ = layui.jquery;
    var temp = $("#" + formId).serializeArray();
    var data = {};
    $.each(temp, function (i, array) {
        data[array.name] = array.value;
    });
    return data;
}

function formatTemplate(dta, tmpl) {
    var format = {
        name: function (x) {
            return x
        }
    };
    return tmpl.replace(/{(\w+)}/g, function (m1, m2) {
        if (!m2)
            return "";
        return (format && format[m2]) ? format[m2](dta[m2]) : dta[m2];
    });
}

layui.use(['layer', 'jquery', 'util'], function () {
    var $ = layui.jquery, util = layui.util, layer = layui.layer;
    util.fixbar({
        showHeight: 50
    });

    $.ajaxSetup({
        dataType: "json",
        cache: false,
        headers: {
            "token": localStorage.getItem("token")
        },
        complete: function (xhr) {
            //token过期，则跳转到登录页面
            if (xhr.status !== 200) {
                layer.msg('登录已失效，请重新登录', {
                    shift: 6
                });
            }
        },
        error: function(jqXHR, textStatus, errorMsg){
            layer.msg(textStatus + errorMsg, {
                shift: 6
            });
        }

    });

    var $blogMask = $('.blog-mask'), $blogNavLeft = $('.blog-nav-left');
    //侧边导航开关点击事件
    $('.blog-navicon').click(function () {
        var sear = new RegExp('layui-hide');
        if (sear.test($blogNavLeft.attr('class'))) {
            leftIn();
        } else {
            leftOut();
        }
    });
    //侧边导航遮罩点击事件
    $blogMask.click(function () {
        leftOut();
    });

    //显示侧边导航
    function leftIn() {
        $blogMask.unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');
        $blogNavLeft.unbind('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend');

        $blogMask.removeClass('maskOut');
        $blogMask.addClass('maskIn');
        $blogMask.removeClass('layui-hide');
        $blogMask.addClass('layui-show');

        $blogNavLeft.removeClass('leftOut');
        $blogNavLeft.addClass('leftIn');
        $blogNavLeft.removeClass('layui-hide');
        $blogNavLeft.addClass('layui-show');
    }

    //隐藏侧边导航
    function leftOut() {
        $blogMask.on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
            $blogMask.addClass('layui-hide');
        });
        $blogMask.on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
            $blogNavLeft.addClass('layui-hide');
        });

        $blogMask.removeClass('maskIn');
        $blogMask.addClass('maskOut');
        $blogMask.removeClass('layui-show');

        $blogNavLeft.removeClass('leftIn');
        $blogNavLeft.addClass('leftOut');
        $blogNavLeft.removeClass('layui-show');
    }

    //定位显示过的标题
    $(function () {
        var ind = cookie.get("titleIndex");
        if (typeof(ind) === "undefined") {
            ind = 0;
        }
        $("#title-nav").find('a').eq(ind).attr("style", "color: #45B6F7;");
    });
});
